package com.ironman.compute.consumer;

import com.ironman.common.params.SpeedParam;
import com.ironman.common.util.AccuracyUtil;
import com.ironman.common.util.UnitConvertUtil;
import com.ironman.event.properties.SysTrainingProperties;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.client.MqttClient;
import org.noear.snack.ONode;

import java.util.function.Consumer;

@Slf4j
public class SpeedAndVmgAndWindConsumer extends BaseConsumer implements Consumer<ONode> {
    private Double preSpeed = null;

    private final SpeedParam speedParam;

    public SpeedAndVmgAndWindConsumer(SpeedParam speedParam, SysTrainingProperties sysTrainingProperties, MqttClient mqttClient) {
        super(sysTrainingProperties, mqttClient);
        this.speedParam = speedParam;
    }

    @Override
    public void accept(ONode data) {
        doAccept("speed-vmg-wind", data, (topic, dataJson, published) -> log.info("{} {} \n{}", topic, published, dataJson));
    }

    @Override
    public boolean threshold(ONode data) {
        if (data.select("speed").isNull()) {
            return false;
        }
        double speed = data.select("speed").getDouble();

        if (preSpeed == null) {
            getCounter().incrementAndGet();
            preSpeed = speed;
            data.set("tt", "t");
            return true;
        }
        double diff = Math.abs(speed - preSpeed);
        double unitConvert = UnitConvertUtil.unitConvert(speedParam.getSpeedThreshold(), speedParam.getSpeedUnit());
        if (diff > unitConvert) {
            getCounter().set(0);
            log.info("船速触发对比阈值:{} 当前:{} 前一个:{} 相差:{} ", unitConvert, speed, preSpeed, diff);
            preSpeed = speed;
            data.set("tt", "t");
            return true;
        }

        Integer countThreshold = speedParam.getCountThreshold();
        if (getCounter().incrementAndGet() >= countThreshold) {
            getCounter().set(0);
            log.info("船速触发数量阈值:{} ", countThreshold);
            preSpeed = speed;
            data.set("tt", "c");
            return true;
        }
        return false;
    }

    @Override
    public void precision(ONode data) {
        double speed = data.select("speed").getDouble();
        data.set("speed", AccuracyUtil.halfUpSpeedDouble(speed));

        if (!data.select("standardSpeed").isNull()) {
            double standardSpeed = data.select("standardSpeed").getDouble();
            data.set("standardSpeed", AccuracyUtil.halfUpSpeedDouble(standardSpeed));
        }
        if (!data.select("customStandardSpeed").isNull()) {
            double customStandardSpeed = data.select("customStandardSpeed").getDouble();
            data.set("customStandardSpeed", AccuracyUtil.halfUpSpeedDouble(customStandardSpeed));
        }
        if (!data.select("angle").isNull()) {
            double angle = data.select("angle").getDouble();
            data.set("angle", AccuracyUtil.downDirectionInteger(angle));
        }
        if (!data.select("vmg").isNull()) {
            double vmg = data.select("vmg").getDouble();
            data.set("vmg", AccuracyUtil.halfUpSpeedDouble(vmg));
        }
        if (!data.select("standardVmg").isNull()) {
            double standardVmg = data.select("standardVmg").getDouble();
            data.set("standardVmg", AccuracyUtil.halfUpSpeedDouble(standardVmg));
        }
        if (!data.select("customAngle").isNull()) {
            double customAngle = data.select("customAngle").getDouble();
            data.set("customAngle", AccuracyUtil.downDirectionInteger(customAngle));
        }
        if (!data.select("customVmg").isNull()) {
            double customVmg = data.select("customVmg").getDouble();
            data.set("customVmg", AccuracyUtil.halfUpSpeedDouble(customVmg));
        }
        if (!data.select("customStandardVmg").isNull()) {
            double customStandardVmg = data.select("customStandardVmg").getDouble();
            data.set("customStandardVmg", AccuracyUtil.halfUpSpeedDouble(customStandardVmg));
        }
    }
}
